<?php
App::uses('AppController', 'Controller');
App::uses('Component', 'Controller');
class ConsultantsController extends AppController{
    public $uses = array('Commit','User','Comments','UserCat','VoteTransac');
    function beforeFilter(){
        parent::beforeFilter();
        $this->layout = 'consultants';
    }
    /**
     * Function index
     * @author MinhPhan
     */
    function index(){
    	//User đăng nhập từ ngoài vào trang chuyên gia
    	if($this->request->query){
    		//get uid của chuyên gia
    		$uid = $this->request->query['uid'];
    		$userInfo = $this->User->find('first',array('conditions'=>array('id'=>$uid)));
    		$userInfo = $userInfo['User'] ;//get thông tin của chuyên gia
    		$arrCatUser = $this->getcategorybyuser($uid); //get category của chuyên gia
    		$consultingFees = $userInfo['consulting_fees']; //phí giao dịch của chuyên gia
    		$this->set('userInfoLayout',$userInfo);
    		if($this->request->is('post')){
    			if(isset($this->request->data['reply'])){
    				$CommitId 		= $this->request->data('CommitId');
    				$user_id_comit 	= $this->request->data('user_id_comit');
    				$Commit   		= $this->request->data('reply');
    				$user_id 		= $this->Auth->user('id');
    				$user_avata 	= $this->Auth->user('avata');
    				$arrDataReply = array(
    						'stats_id' =>$CommitId,
    						'user_id'  =>$user_id_comit,
    						'user_comment_id' =>$user_id,
    						'comment'	=>$Commit,
    						'user_name_comments' => $this->Auth->user('username'),
    						'create_time' =>time(),
    						'update_time' =>time(),
    						'avata_user_comment' =>$user_avata
    				);
    				$this->Comments->save($arrDataReply);
    			}
    		}
    		
    		//get Note của chuyên gia
    		$arrNote = $this->Commit->find('all',array(
    				'conditions'=>array('user_id' => $uid),
    				'order'=>'time_create DESC'
    		));
    		
    		if(!empty($arrNote)){
    			foreach ($arrNote as $item){
    				$arrComment = $this->Comments->find('all',array('conditions'=>array(
    						'user_id' => $uid,
    						'stats_id' =>$item['Commit']['id']
    				),
    						'order'=>'create_time DESC'
    				));
    				$arrDataResult[] = array(
    						'Commit' => $item,
    						'Comment' => $arrComment
    				);
    			}
    			$this->set('arrCommit',$arrDataResult);
    		}
    		
    		//set value for view
    		$this->set('Permission','1');
    		$this->set('checkreply','1');
    		$arrCatUser = $this->getcategorybyuser($uid);
    	}else{
    		$userInfo = $this->Auth->user();
    		$uid = $this->Auth->user('id');
    		$userCurrent = $this->Auth->user();
    		$consultingFees = $this->Auth->user('consulting_fees');
	    	if($this->request->is('post')){
	    		if(isset($this->request->data['reply'])){ 
	    			$CommitId 		= $this->request->data('CommitId');
	    			$user_id_comit 	= $this->request->data('user_id_comit');
	    			$Commit   		= $this->request->data('reply');
	    			$user_id 		= $this->Auth->user('id');
	    			$user_avata 	= $this->Auth->user('avata');
	    			$arrDataReply = array(
	    					'stats_id' =>$CommitId,
    						'user_id'  =>$user_id_comit,
    						'user_comment_id' =>$user_id,
    						'comment'	=>$Commit,
    						'user_name_comments' => $this->Auth->user('username'),
    						'create_time' =>time(),
    						'update_time' =>time(),
    						'avata_user_comment' =>$user_avata
	    					);
	    			$this->Comments->save($arrDataReply);
	    		}
	    		if(isset($this->request->data['comment'])){
	    			$arrNotes = $this->request->data;
		    		$arrData = array(
		    				'title' 		=> 	$arrNotes['tieude'],
		    				'content' 		=>	$arrNotes['comment'],
		    				'total_comment' => 0,
		    				'status'		=> 0,
		    				'user_id'		=> $uid,
		    				'time_create' 	=> time(),
		    				'time_update' 	=> time()
		    		);
		    		$this->Commit->save($arrData);
		    	}
	    	}
	    	
	    	$arrCatUser = $this->getcategorybyuser($uid);
	    	//get note by user
	    	$userInfo = $this->Auth->user();
	    	$arrNote = $this->Commit->find('all',array(
	    												'conditions'=>array('user_id' => $uid),
	    												'order'=>'time_create DESC'
	    											 ));
	    	if(!empty($arrNote)){
		    	foreach ($arrNote as $item){
		    		$arrComment = $this->Comments->find('all',array('conditions'=>array(
		    															'user_id' => $uid,
		    															'stats_id' =>$item['Commit']['id'])));
		    		$arrDataResult[] = array(
					    					'Commit' => $item,
					    					'Comment' => $arrComment
		    								);
		    	}
		    	$this->set('arrCommit',$arrDataResult);
	    	}
    }
    	$this->set('currentUser',$this->Auth->user());
    	$this->set('uid',$uid);
	    $this->set('userinfo',$userInfo);
	    $this->set('consultingFees',$consultingFees);
	    $this->set('arrCat',$arrCatUser);
    }
    /**
     * 
     */
    public function getcategorybyuser($uid){
    	$arrCatByUser = $this->UserCat->find('all',array('conditions'=>array('user_id'=>$uid)));
    	return $arrCatByUser;
    	
    }
    function ajaxUpdatename(){
    	$fullname = $this->request->data('fullname'); 
    	if($this->User->updatebyConditon($fullname,$this->Auth->user('id'))){
    		$arrDataReturn = json_encode(array('code'   =>  1));
    	}else{
    		$arrDataReturn = json_encode(array('code'   =>  0));
    	}
    	$this->response->body($arrDataReturn);
    	return $this->response;
    	
    }
    
    public function vote(){
    	$arrVote = $this->VoteTransac->find('all',array('conditions'=>array('consu_id' => $this->Auth->user('id'))));
    	foreach ($arrVote as $itemvote){
    			$uservoteinfo = $this->User->find('first',array('conditions'=>array('id'=>$itemvote['VoteTransac']['user_id'])));
    			$class = "";
    			$star = 0;
    			if($itemvote['VoteTransac']['total_rate'] != 0)
    				$star = round($itemvote['VoteTransac']['total_star']/$itemvote['VoteTransac']['total_rate'],0);
    			if($star == 0){
    				$class = array('star-no-view');
    			}elseif($star == 5){
    				$class = array('star-view');
    			}else{
    				for($i=0;$i<$star;$i++){
    					$class[] = 'star-view-one';
    				}
    				for($i=0;$i<(5-$star);$i++){
    					$class[] = 'star-no-view-one';
    				}
    			}
    			$arrResult[] = array(
    							'name' =>$uservoteinfo['User']['full_name'],
    							'img' =>$uservoteinfo['User']['avata'],
    							'star_class' =>$class,
    							'comments' =>$itemvote['VoteTransac']['comments'],
    							'total_star'=>$itemvote['VoteTransac']['total_star'],
    							'create' =>$this->unix_to_gmt_sevent($itemvote['VoteTransac']['create_time']),
    							
    					);
    			
    	}
    	
    	$this->set('arrVote',$arrResult);
    }
    
    function unix_to_gmt_sevent($timenow) {
    	$str_in = array ( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",              "am", "pm",            ":"    );
    	$str_out = array ( "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy", "Chủ nhật", " phút (sáng)", " phút (chiều)", " giờ "    );
    	$time  = gmdate("D, d-m-Y g:i a", $timenow + 7*3600);
    	$time  = str_replace( $str_in, $str_out, $time);
    	return $time;
    }
    
}

?>